// export function useTabBarBadge () {}
import { onShow } from '@dcloudio/uni-app'
import { watch } from 'vue'
import { useCartStore } from '@/store/cart'
import { useUserStore } from '@/store/user'
const store = useCartStore()
const userStore = useUserStore()

// 设置tabBar的badge
export const useTabBarBadge = () => {
  // 跟徽标相关
  onShow(() => {
    if (store.getTotal > 0) {
      uni.setTabBarBadge({
        index: 2,
        text: store.getTotal + ''
      })
    } else {
      uni.removeTabBarBadge({
        index: 2
      })
    }
  })

  watch(
    () => store.getTotal,
    newValue => {
      if (newValue > 0) {
        uni.setTabBarBadge({
          index: 2,
          text: newValue + ''
        })
      } else {
        uni.removeTabBarBadge({
          index: 2
        })
      }
    },
    {
      immediate: true
    }
  )
}

// 设置tabBar某一项的文字
export const useTabBarTitle = (
  index: number,
  loginedTitle: string,
  unloginTitle: string
) => {
  // 根据是否登录来设置某一项的文字
  onShow(() => {
    if (userStore.user.token) {
      uni.setTabBarItem({
        index,
        text: loginedTitle
      })
    } else {
      uni.setTabBarItem({
        index,
        text: unloginTitle
      })
    }
  })
}
